package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.FacilityInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "配套管理")
@RestController
@RequestMapping("/admin/facility")
public class FacilityController {

    @Autowired
    private FacilityInfoService facilityInfoService;

    /**
     * todo:逻辑：根据动态条件，查询配套信息列表
     *          type:1|2|null
     * todo:实现步骤：
     *          1.创建动态条件对象
     *          2.根据动态条件，执行查询方法
     *          3.将查询列表装到Result.ok()
     *          4.返回result对象
     * @param type
     * @return
     */
    @Operation(summary = "[根据类型]查询配套信息列表")
    @GetMapping("list")
    public Result<List<FacilityInfo>> listFacility(@RequestParam(required = false) ItemType type) {
        LambdaQueryWrapper<FacilityInfo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null,FacilityInfo::getType,type);
        List<FacilityInfo> list = facilityInfoService.list(queryWrapper);
        Result<List<FacilityInfo>> result = Result.ok(list);
        return result;
    }

    /**
     * todo：逻辑；
     *          根据对象中id是否存在，新增或修改配套信息，
     *          判定执行结果，并返回
     * @param facilityInfo
     * @return
     */
    @Operation(summary = "新增或修改配套信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody FacilityInfo facilityInfo) {
        boolean is = facilityInfoService.saveOrUpdate(facilityInfo);
        if(!is){
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * todo：逻辑；根据id，删除配套信息，返回执行结果result
     * @param id
     * @return
     */
    @Operation(summary = "根据id删除配套信息")
    @DeleteMapping("deleteById")
    public Result removeFacilityById(@RequestParam Long id) {
        facilityInfoService.removeById(id);
        return Result.ok();
    }

}
